/*
 * @Author: LVGRAPE
 * @Date: 2023-09-11 14:35:28
 * @LastEditTime: 2023-09-13 10:06:37
 * @LastEditors: LVGRAPE
 * @Description:
 * @FilePath: \ZINO_FC_V4\ZINO\hardware\spl06\spl_test.c
 * 要啥没啥，爱咋咋的
 */
#include <rtthread.h>
#include <rtdevice.h>
#include "spl06_01.h"
#include "zino.h"
void spl06(int argc, char* argv[])
{
    static spl0601_t* spl06_dev = RT_NULL;
    if (argc == 2)
    {
        spl06_dev = rt_malloc(sizeof(spl0601_t));
        if (spl0601_init(spl06_dev, argv[1]) == RT_EOK)
        {
            rt_kprintf("SPL0601 init success\n");

            spl0601_start_pressure(spl06_dev);
            rt_thread_delay(5);
            spl0601_get_raw_pressure(spl06_dev);
            int pres = spl0601_get_pressure(spl06_dev) * 1000;
            rt_kprintf("pressure:%d.%03d\n", pres/1000, pres%1000);
        }
    }
    else
    {
        // struct rt_spi_configuration cfg = {
        // .data_width = 8,
        // .mode = RT_SPI_MASTER | RT_SPI_MSB | RT_SPI_MODE_3,
        // .max_hz = 10 * 1000 * 1000,//NOTEE SPL06 SPI CLOCK MAX 10MHZ,
        // };
        if(!spl06_dev) return;
        // spl06_dev->bus->config.data_width = 8;
        // spl06_dev->bus->config.mode = RT_SPI_MASTER | RT_SPI_MSB | RT_SPI_MODE_3;
        // spl06_dev->bus->config.max_hz = 10 * 1000 * 100;
        rt_spi_configure(spl06_dev->bus, &(spl06_dev->bus->config));
        spl0601_start_pressure(spl06_dev);
        rt_thread_delay(100);
        spl0601_get_raw_pressure(spl06_dev);
        int pres = spl0601_get_pressure(spl06_dev)*1000.f;
        rt_kprintf("pressure:%d.%03d\n", pres/1000, pres%1000);

        spl0601_start_temperature(spl06_dev);
        rt_thread_delay(100);
        spl0601_get_raw_temp(spl06_dev);
        int temp = spl0601_get_temperature(spl06_dev)*1000.f;
        rt_kprintf("temperatur:%d.%03d\n", temp/1000,temp%1000);
    }
}
void spl06_rountine_entry()
{
    static spl0601_t* spl06_dev = RT_NULL;
    spl06_dev = rt_malloc(sizeof(spl0601_t));
    if (spl0601_init(spl06_dev, "spl06") == RT_EOK)
    {
        spl0601_start_continuous(spl06_dev,SPL06_MODE_SPORTS);

    }
    while (1)
    {
        spl06_dev->bus->config.data_width = 8;
        spl06_dev->bus->config.mode = RT_SPI_MASTER | RT_SPI_MSB | RT_SPI_MODE_3;
        spl06_dev->bus->config.max_hz = 10 * 1000 * 100;
        rt_spi_configure(spl06_dev->bus, &(spl06_dev->bus->config));

        spl0601_get_raw_pressure(spl06_dev);
        spl0601_get_raw_temp(spl06_dev);

        int temp = spl0601_get_temperature(spl06_dev)*1000.f;
        int pres = spl0601_get_pressure(spl06_dev)*1000.f;

        
        rt_kprintf("P:%d.%03d \t T:%d.%03d \n",pres/1000, pres%1000, temp/1000,temp%1000);

        rt_thread_delay(20);
    }
    
}
int spl06_rountine()
{
    rt_thread_t tid = rt_thread_create("spl06", spl06_rountine_entry, RT_NULL, 2048, 25, 10);
    if(tid)
    {
        rt_thread_startup(tid);
    }
    return 0;
}
// ZINO_APP_EXPORT(spl06_rountine);
// MSH_CMD_EXPORT(spl06, SPL0601 init test);